﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;
using System.Data;

namespace IHISCETeim.Web.MachineSys.MacModel
{
    public partial class SupplierShiYanTongJi : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.查看 };
            }
        }
        protected override string DWZRef
        {
            get
            {
                return "SupplierShiYanTongJi";
            }
        }

        protected string fromdate
        {
            get
            {
                return GetRequestValue<string>("fromdate","");
            }
        }

        protected string todate
        {
            get
            {
                return GetRequestValue<string>("todate","");
            }
        }

        protected string fromClaimsNumber
        {
            get
            {
                return GetRequestValue<string>("fromClaimsNumber", "");
            }
        }

        protected string toClaimsNumber
        {
            get
            {
                return GetRequestValue<string>("toClaimsNumber", "");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.rpMenu.DataSource = GetData();
            this.rpMenu.DataBind();
        }

        public class ShiYanInfo
        {
            public string SupplierName { get; set; }
            /// <summary>
            /// 次数
            /// </summary>
            public int ChiSu { get; set; }
            /// <summary>
            /// 小于等于50
            /// </summary>
            public int Chi50 { get; set; }
            /// <summary>
            /// 大于50小于500小时
            /// </summary>
            public int Chi500 { get; set; }
            /// <summary>
            /// 大于500　小于2000
            /// </summary>
            public int ChiThen500 { get; set; }
        }

        private string GetSqlWhere()
        {
            string wheresql = " 1=1 ";
            string DOWNSQL = "";
            if (!string.IsNullOrEmpty(fromdate))
            {
                DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("FaultDate", QueryOperatorsEnum.范围, fromdate + "|" + todate);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }



            if (!string.IsNullOrEmpty(fromClaimsNumber))
            {
                DOWNSQL = QueryOperatorsEnumHelper.ConvertCommonOperator("ClaimsNumber", QueryOperatorsEnum.范围, fromClaimsNumber + "|" + toClaimsNumber);
                if (!string.IsNullOrEmpty(wheresql))
                {
                    wheresql += " and " + DOWNSQL;
                }
                else
                {
                    wheresql = DOWNSQL;
                }
            }


            return wheresql;
        }

        public List<ShiYanInfo> GetData()
        {
            DataTableCollection dtc = UClaimDemageControlController.SupplierShiYanTongJi(GetSqlWhere());
            DataTable tempdt = dtc[0];
            //DutyPanDing ,WorkHour
            Dictionary<string, ShiYanInfo> list = new Dictionary<string, ShiYanInfo>();
            foreach (DataRow dr in tempdt.Rows)
            {
                string DutyPanDing = dr["DutyPanDing"].ToString();
                decimal WorkHour = Convert.ToDecimal( dr["WorkHour"].ToString());

                if (string.IsNullOrEmpty(DutyPanDing))
                    continue;

                string[] DutyPanDingArr = DutyPanDing.Split(new char[]{ ','});


                foreach (string item in DutyPanDingArr)
                {
                    ShiYanInfo tempInfo = new ShiYanInfo();
                    if (list.ContainsKey(item))
                    {
                        tempInfo = list.First(e => e.Key == item).Value;
                        tempInfo.ChiSu = tempInfo.ChiSu + 1;                        
                    }
                    else
                    {
                        tempInfo = new ShiYanInfo()
                        {
                            SupplierName = item,
                            ChiSu=1
                        };
                        list.Add(item, tempInfo);
                    }


                    //判断所在时间段
                    if (WorkHour <= 50)
                    {
                        tempInfo.Chi50++;
                    }
                    if (WorkHour > 50 && WorkHour <= 500)
                    {
                        tempInfo.Chi500++;
                    }
                    if (WorkHour > 500　&& WorkHour<=2000)
                    {
                        tempInfo.ChiThen500++;
                    }
                    
                }
                
            }
            //去掉IHISCE责任
            List<ShiYanInfo> rList = list.Values.ToList<ShiYanInfo>();
            string[] strarr = new string[] {"","请选择...." ,"厂内组装", "营业政策性索赔", "特殊索赔约定", "其它", "专案件", "试制件" };
            foreach (string item in strarr)
            {
                try
                {
                    ShiYanInfo info = rList.First(e => e.SupplierName == item);
                    if (info != null)
                        rList.Remove(info);
                }
                catch
                { }
            }
            //
            return rList;
          
        }
    }
}